package com.cf.moudle.newsfeed.mvp.presenter;

import com.cf.base.http.HttpManager;
import com.cf.base.mvp.mvp.BasePresenter;
import com.cf.moudle.newsfeed.mvp.api.UserApi;
import com.cf.moudle.newsfeed.mvp.bean.BaseInfo;
import com.cf.moudle.newsfeed.mvp.bean.UserInfo;
import com.cf.moudle.newsfeed.mvp.contracts.MainContract;
import com.cf.moudle.newsfeed.mvp.model.MainModel;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.observers.DefaultObserver;
import io.reactivex.schedulers.Schedulers;

/**
 * @author yangji
 */
public class MainPresenter extends BasePresenter<MainModel, MainContract.View> implements MainContract.Presenter {

    @Override
    public void requestLogin(String name, String pwd) {

        //如果使用 model
        getModel().login(name, pwd);

        //如果不使用
        HttpManager.getInstance().create(UserApi.class)
                .login(name, pwd)
                .compose(bindEvent())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new DefaultObserver<BaseInfo<UserInfo>>() {
                    @Override
                    public void onNext(BaseInfo<UserInfo> userInfoBaseInfo) {
                        getMvpView().loginSuccess();
                    }

                    @Override
                    public void onError(Throwable e) {
                        getMvpView().loginFailed();
                    }

                    @Override
                    public void onComplete() {

                    }
                });

    }


    /**
     * 配合Mvp Model 使用
     */
    @Override
    public void responseLogin(UserInfo info) {
        if (info != null) {
            getMvpView().loginSuccess();
        } else {
            getMvpView().loginFailed();
        }
    }
}
